home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / tpshare5.arc / TPSHARE5.TXT < prev   
Text File  |  1991-04-28  |  21KB  |  529 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                         Methods for Dealing with Shared Files
  8.                              in TURBO Pascal Version 5.0
  9.                                           by
  10.                                     John W. Wulff
  11.                                Wulff Enterprises, Inc.
  12.                                  260 Terranova Drive
  13.                               Warrenton, VA  22186-9227
  14.                                     (703) 349-8805
  15.  
  16.                                  Revised May 15, 1989
  17.            
  18.           This paper was first written January 19, 1987 as a means of
  19.           demonstrating how to modify Turbo Pascal 3.x to handle the
  20.           opening of READ-ONLY files and to make it able to deal with files
  21.           in a Shared, network environment.  It provided a means of finding
  22.           an undocumented byte known as the "Open Mode Byte".  Since then,
  23.           Turbo Pascal 5.0 has been released and this byte has been
  24.           globally declared as the FILEMODE variable.  Even so, Turbo
  25.           Pascal 5.0 ignores the variable when opening TEXT files.  While
  26.           there is a small explanation in the Turbo Pascal Reference Guide
  27.           as to its use, the information in this paper is still valid today
  28.           and has been updated to reflect its relevance to Turbo Pascal
  29.           5.0.  For those wishing to continue to work in Turbo Pascal 3.0,
  30.           I have included the locations for this byte in appendix A.
  31.  
  32.           There have been many innovations in the development of PCBoard
  33.           software for BBS's.  One of which is the network environment that
  34.           the authors have included in the code.  While this is wonderful
  35.           for the multi-node system, it does pose certain problems and
  36.           restrictions on application programs and utilities that are being
  37.           written for it, especially those written in Borland's Turbo
  38.           Pascal.
  39.  
  40.           One problem that plagues most authors writing application
  41.           programs that eventually become used in network environments is
  42.           coping with the Share utility that DOS uses to protect files. 
  43.           Any file opened for reading under Turbo Pascal will cause a
  44.           Sharing violation if running under a DOS 3.x and 4.x networking
  45.           mode.  This is because Turbo Pascal opens all files in what is
  46.           known as "Inherited by Child Processes, Compatibility Mode,
  47.           Read/Write access". 
  48.  
  49.           I have written a utility for PCBoard systems, PCBFile, and since
  50.           it is extremely file intensive, I've had to do some research on
  51.           the technical aspects of DOS in the network mode.  Because of
  52.           this research, I've been able to get Turbo Pascal to cooperate
  53.           and have written this paper to help other authors who are
  54.           struggling with the same problems.
  55.  
  56.           As documented in the Turbo Pascal instructions, the FILEMODE
  57.           variable has a value of 2 when opening files for RESET or REWRITE
  58.           which allows both reading and writing.  The instructions suggest
  59.           that you should assign a value of 0 to the variable to RESET or
  60.           REWRITE read-only files.  While this works for READ-ONLY files,
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.           it is not the only value to consider when running an application
  74.           in a network environment, especially one with PCBoard software
  75.           upon which I will focus my attention for the remainder of the
  76.           document.
  77.            
  78.           One thing to consider, if using other languages, especially
  79.           assembly language, is that this FILEMODE variable, corresponds to
  80.           the AL register.  All references to that byte and its decimal
  81.           number can be applied to assembly in this way: 
  82.  
  83.                  AH contains 3DH - the function call 
  84.                  DS:DX points to an ASCIIZ path name 
  85.                  AL will be loaded with the 8 bit number that the          
  86.                  FILEMODE variable contains. 
  87.  
  88.           When the function returns, AX will contain error codes or a 16
  89.           bit file handle if successful.  (See DOS manual for details.)  I
  90.           don't profess to be anywhere close to fluent in assembly so I
  91.           will leave this information with those who are best suited to
  92.           take advantage of it. 
  93.  
  94.           This variable is GLOBALLY pre-declared in Turbo Pascal, so all
  95.           you have to do is refer to it as FILEMODE.  Then a simple check
  96.           is necessary to see if the variable contains a 2 and if so, load
  97.           it with a 0: 
  98.            
  99.                          begin 
  100.                            if FILEMODE = 2 then 
  101.                              FILEMODE := 0; 
  102.                          end;
  103.  
  104.           The completed routine would look like this:  
  105.  
  106.           Procedure OpenMode(Var OpenFile : text; 
  107.                                InFileName : string
  108.                              Var GoAhead  : boolean); 
  109.  
  110.           {Remember, FILEMODE is a GLOBALLY declared variable of type
  111.            byte.  No other declaration is necessary on your part}
  112.  
  113.           begin 
  114.             if FILEMODE = 2 then 
  115.               FILEMODE := 0; (* allows reading of READ-ONLY files *) 
  116.             assign(OpenFile,InFileName); 
  117.             {$I-} reset(Openfile) {$I+}; 
  118.             GoAhead := (ioresult = 0);   
  119.             if GoAhead then 
  120.               writeln(InFileName,' opened!') 
  121.             else 
  122.               writeln(InFileName,' failed to open!'); 
  123.           end; { of Procedure OpenMode } 
  124.  
  125.  
  126.           Shared Files in Turbo Pascal 5.0                                2
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.           Now we need to determine just what is going on with PCBoard and
  140.           how it opens files using DOS's SHARE.   The DIR files, or the
  141.           files that contain the filenames of the available files for the
  142.           user to download, are opened in READ SHARED mode which in
  143.           QuickBASIC would be: 
  144.  
  145.               OPEN "<filename>" FOR INPUT ACCESS READ SHARED AS #1.   
  146.  
  147.           Upload DIR files are opened for APPEND in a LOCKED WRITE mode.
  148.           This keeps other nodes from writing at that particular moment but
  149.           allows other nodes to read this file.
  150.  
  151.           In order to accomplish these same modes in Turbo Pascal we must
  152.           look into the DOS Technical Reference Manual (groan!).  The
  153.           following is reprinted from that manual with some additions by
  154.           me.
  155.  
  156.           The OPEN mode consists of 4 bit-oriented fields:     
  157.                * INHERITANCE FLAG
  158.                * SHARING MODE FIELD 
  159.                * RESERVED FIELD 
  160.                * ACCESS FIELD   
  161.           The INHERITANCE FLAG determines if the file will ever be
  162.           inherited by another process, which in the case of a network is
  163.           usually the desired effect.  The SHARING FIELD defines what
  164.           operations may be performed on the file by other nodes.  The
  165.           ACCESS FIELD defines what operations THIS node may perform on the
  166.           file.  
  167.  
  168.           The bit fields are mapped as follows: 
  169.                               <I> < S > <R> < A > 
  170.              Open Mode bits    7  6 5 4  3  2 1 0 
  171.           I  INHERITANCE FLAG 
  172.              If I = 0; File is inherited by child processes 
  173.              If I = 1; File is private to the current process 
  174.  
  175.           S  SHARING MODE 
  176.              The file is opened like this:
  177.              if S = 000;  Compatibility mode - The default open mode - it
  178.                           denies ALL OTHER processes access to the file. 
  179.                           Since this is the mode that Turbo Pascal uses to
  180.                           open a file, what do you think will happen on the
  181.                           BBS side if you have a file open on your end and
  182.                           the BBS tries to open it? 
  183.              if S = 001;  Deny Read/Write mode (Exclusive).  This would
  184.                           actually be the same as setting the I flag to 1. 
  185.              if S = 010;  Deny Write mode - you should open a file in this
  186.                           mode if you wish to protect it.  It will allow
  187.                           other processes to read it but not write.
  188.              if S = 011;  Deny Read mode 
  189.              if S = 100;  Deny None mode - Who cares what happens!
  190.  
  191.  
  192.           Shared Files in Turbo Pascal 5.0                                3
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.           It is important to specify what operations you want to perform
  206.           (access mode).  The default access mode is Read/Write and causes
  207.           the open request to fail if another process has the file opened
  208.           with any sharing mode other than Deny None.  File sharing
  209.           requires cooperation of both sharing processes.  This is
  210.           communicated through the sharing and access modes.
  211.  
  212.           R  RESERVED (set third bit field to 0)
  213.  
  214.           A  ACCESS - The file access is assigned as follows:
  215.  
  216.              If A = 000; Read Access 
  217.              if A = 001; Write Access 
  218.              if A = 010; Read/Write access
  219.  
  220.           If all this seems a bit involved, I'm sorry.  I don't know of any
  221.           way to give you the background for all this hocus-pocus except
  222.           with the above info.  I also recommend picking up a Tech Ref
  223.           manual for more detailed study of the 3DH function call.
  224.  
  225.           OK!  With all these numbers in hand, let's see how to get Turbo
  226.           Pascal to duplicate these modes.  Earlier I said that other gurus
  227.           had stated that Turbo Pascal opens files in COMPATIBILITY MODE
  228.           with READ/WRITE ACCESS and INHERITANCE BY CHILD PROCESSES and the
  229.           magic value for the FILEMODE variable is 2.  Let's look at how
  230.           that was done:
  231.  
  232.                Compatibility mode:   000 {S} 
  233.                Read/Write ACCESS :   010 {A} 
  234.                Inherited by child:     0 {I} 
  235.                Reserved as ALWAYS:     0 {R} 
  236.            
  237.           Remember the bit fields are: 
  238.                               <I> < S > <R> < A > 
  239.              Open Mode bits    7  6 5 4  3  2 1 0  Let's plug in 
  240.                                0  0 0 0  0  0 1 0  the numbers. 
  241.           Using binary arithmetic: 
  242.                               <I> <  S   > <R> < A >
  243.                               128 64 32 16  8  4 2 1
  244.                                0   0  0  0  0  0 1 0 = 00000010 = 2 
  245.             
  246.           By using a FILEMODE value of 0 we change the ACCESS field to 000,
  247.           READ ACCESS, which allows us to read a READ-ONLY file.
  248.            
  249.           PCBoard is opening its DIR files as READ ACCESS SHARED and
  250.           actually opening the file with a SHARING MODE of Deny/Write which
  251.           would be a SHARE <S> field of 010.  The value for the FILEMODE
  252.           variable then becomes: 
  253.                               <I> <  S   > <R> < A > 
  254.                               128 64 32 16  8  4 2 1 
  255.                                0   0  1  0  0  0 0 0 = 00100000 = 32
  256.  
  257.  
  258.           Shared Files in Turbo Pascal 5.0                                4
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.           This is how I open ALL my files for reading or for general
  272.           ASSIGNING for RESET in Turbo Pascal for my program PCBFile.  I
  273.           have some procedures written for TEXT files, and files of type
  274.           BYTE.  I have reproduced the code below:  
  275.           (* This procedure, KeepOn, is used to determine if the file has 
  276.           been locked out.  I try a file for 10 times as determined by 
  277.           OpenAtt variable before I give up  *) 
  278.            
  279.           Procedure KeepOn(OpenAtt : byte; var GA : boolean); 
  280.           begin 
  281.             if OpenAtt <= 10 then 
  282.               GA := TRUE      (* GoAhead Flag - if within 10 go for it *) 
  283.             else GA := FALSE;    (* forget it! *) 
  284.           end; {of Procedure KeepOn}
  285.            
  286.           Procedure SetResetMode(Var OpenFile : text;  (* OPEN MODE FOR
  287.                                  InFileName   : string;    TEXT FILES *) 
  288.                                   Var GoAhead : boolean); 
  289.           var
  290.             OpenAttempts  : byte; 
  291.  
  292.           begin 
  293.             OpenAttempts := 1; 
  294.             FILEMODE := 32;         (* this is Deny Write                  
  295.                                        Mode/Read Access *) 
  296.             assign(OpenFile,InFileName); 
  297.             repeat 
  298.               {$I-} reset(Openfile) {$I+}; 
  299.               GoAhead := (ioresult = 0); 
  300.               if not GoAhead then 
  301.                 OpenAttempts := OpenAttempts + 1; 
  302.             until (GoAhead) OR (OpenAttempts > 10); (* keep trying *) 
  303.             KeepOn(OpenAttempts,GoAhead); 
  304.           end; {of Procedure SetResetMode} 
  305.            
  306.           Procedure SetResetModeFile(Var OpenFile : file of byte 
  307.                                      InFileName   : string;                
  308.                                       var GoAhead : boolean);             
  309.           var 
  310.             OpenAttempts  : byte; 
  311.           begin 
  312.             OpenAttempts := 1; 
  313.             if FILEMODE := 32;   (* this is Deny Write                     
  314.                                   Mode/Read Access *)    
  315.           assign(OpenFile,InFileName); 
  316.             repeat 
  317.               {$I-} reset(Openfile) {$I+}; 
  318.               GoAhead := (ioresult = 0); 
  319.               if not GoAhead then OpenAttempts := OpenAttempts + 1; 
  320.             until GoAhead OR (OpenAttempts > 10); 
  321.             KeepOn(OpenAttempts,GoAhead); 
  322.           end; {of Procedure SetResetModeFile}
  323.  
  324.           Shared Files in Turbo Pascal 5.0                                5
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.           Now here comes a little zinger to change things up.  I want to
  339.           create a file that I don't want the other nodes to damage.  I
  340.           elect to open the file for READ/WRITE ACCESS for myself and give
  341.           the other nodes READ capability and deny them the ability to
  342.           write to my file.  This would be Deny/Write Mode under the <S> or
  343.           SHARING FIELD and would be coded 010.  For READ/WRITE ACCESS the
  344.           <A> or ACCESS FIELD is coded 010 also.  This is the same mode
  345.           that PCBoard uses for writing to the Upload directory.  Using our
  346.           binary formulae, the FILEMODE value then becomes: 
  347.                               <I> <  S   > <R> < A > 
  348.                               128 64 32 16  8  4 2 1 
  349.                                0   0  1  0  0  0 1 0 = 00100010 = 34 
  350.  
  351.           With the magic number of 34 the SetFileLock procedure was born.
  352.            
  353.           Procedure SetFileLock(Var   OpenFile : text;
  354.                                     InFileName : string; 
  355.                                    var GoAhead : boolean); 
  356.           var 
  357.             OpenAttempts  : byte;  
  358.  
  359.           begin 
  360.             OpenAttempts := 1; 
  361.             FILEMODE := 34;      (* Deny Write Mode/Read-Write Access *) 
  362.             assign(OpenFile,InFileName); 
  363.             repeat 
  364.               {$I-} rewrite(Openfile) {$I+}; 
  365.               GoAhead := (ioresult = 0); 
  366.               if not GoAhead then 
  367.                 OpenAttempts := OpenAttempts + 1; 
  368.             until GoAhead or (OpenAttempts > 10); 
  369.             KeepOn(OpenAttempts,GoAhead); 
  370.           end; {of Procedure SetFileLock}
  371.            
  372.           Finally, a little walk around the park to insure that the
  373.           FILEMODE variable is returned to Borland's normal mode.
  374.  
  375.           Procedure ReleaseOpenMode; 
  376.  
  377.           begin 
  378.             FILEMODE := 2; 
  379.           end; 
  380.  
  381.           So it's really simple to change the Turbo Pascal Open mode to
  382.           exactly what you want, you just have to know what results you
  383.           desire from the program.  Just remember these definitions of the
  384.           fields that make up the magic number for DOS Function call 3DH. 
  385.            
  386.           * INHERITANCE FLAG       I = 0;   Inherited (usually the case) 
  387.                                    I = 1;   Private
  388.  
  389.  
  390.           Shared Files in Turbo Pascal 5.0                                6
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.           * SHARING MODE FIELD     (Other node or process)
  404.                                    S = 000; Compatibility mode 
  405.                                    S = 001; Deny Read/Write mode           
  406.                                    (Exclusive) 
  407.                                    S = 010; Deny Write mode 
  408.                                    S = 011; Deny Read mode 
  409.                                    S = 100; Deny None mode 
  410.  
  411.           * RESERVED FIELD         R = 0; Always 
  412.  
  413.           * ACCESS FIELD           (Your node or process) 
  414.                                    A = 000; Read Access 
  415.                                    A = 001; Write Access 
  416.                                    A = 010; Read/Write Access
  417.           The bit fields: 
  418.                                    <I> < S > <R> < A >
  419.                   Open Mode bits    7  6 5 4  3  2 1 0 
  420.            
  421.           Even though a DOS Technical Reference Manual gives a more
  422.           thorough discussion of Function Call 3DH, I will attempt to
  423.           create a matrix with the number for the FILEMODE variable based
  424.           on the SHARE and ACCESS fields. 
  425.            
  426.             ACCESS   |SHARE-> Compat  Deny/RW  Deny/W  Deny/R  Deny/N 
  427.              |                 000      001     010     011     100 
  428.              v              ------------------------------------------- 
  429.             Read  000       |   0    |   16  |   32  |   48  |   64   | 
  430.                             |--------|-------|-------|-------|--------| 
  431.             Write 001       |   1    |   17  |   33  |   49  |   65   | 
  432.                             |--------|-------|-------|-------|--------| 
  433.             Read/ 010       |   2    |   18  |   34  |   50  |   66   | 
  434.             Write           ------------------------------------------- 
  435.            
  436.           I know that this is probably more than a human can bear to
  437.           assimilate at any one time but I hope that you will be able to
  438.           see the logic behind my system and be able to use Turbo Pascal to
  439.           its full potential.  
  440.            
  441.           PCBoard (c) Clark Development Company, Inc., Murray, UT 
  442.           Turbo Pascal (c) Borland International, Scotts Valley, CA 
  443.           PCBFile (c) John W. Wulff 
  444.           Compuserve (c) Compuserve, Inc., Columbus, OH 
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.           Shared Files in Turbo Pascal 5.0                                7
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.                                        Appendix
  471.  
  472.           For Turbo Pascal 3.x Die-Hards:
  473.  
  474.           Bela Lubkin published a text article, ACMODE.DOC, which is on
  475.           Compuserve in the Borland Sig that gives the locations of a
  476.           little gem known as the "Open Mode Byte".  This byte is at an
  477.           absolute address for the various editions of Turbo Pascal and
  478.           communicates to DOS, via Function call 3DH, how the file is to be
  479.           accessed and what access to give other processes.  It also
  480.           becomes very handy for us in trying to use Turbo Pascal in a
  481.           network environment. These locations are:
  482.                Open mode byte for Reset & Rewrite for Turbo 3.00x (PC-DOS) 
  483.                     TURBO.COM      CSEG:$248D 
  484.                     TURBO-87.COM   CSEG:$1F3C 
  485.                     TURBOBCD.COM   CSEG:$2393 
  486.               Open mode byte for Reset & Rewrite for Turbo 3.00x (MS-DOS) 
  487.                     TURBO.COM      CSEG:$2182 
  488.                     TURBO-87.COM   CSEG:$1C31 
  489.                     TURBOBCD.COM   CSEG:$2088 
  490.               Open mode byte for Reset & Rewrite for Turbo 3.01x (PC-DOS) 
  491.                     TURBO.COM      CSEG:$24FC 
  492.                     TURBO-87.COM   CSEG:$1FAB 
  493.                     TURBOBCD.COM   CSEG:$2402 
  494.               Open mode byte for Reset & Rewrite for Turbo 3.01x (MS-DOS) 
  495.                     TURBO.COM      CSEG:$21D4 
  496.                     TURBO-87.COM   CSEG:$1C83 
  497.                     TURBOBCD.COM   CSEG:$20DA 
  498.               Open mode byte for Reset & Rewrite for Turbo 3.02x (PC-DOS) 
  499.                     TURBO.COM      CSEG:$24C6 
  500.                     TURBO-87.COM   CSEG:$1F75 
  501.                     TURBOBCD.COM   CSEG:$23CE 
  502.            
  503.           Another valuable document is Robert K. Blaine's RES_MODE.INC,
  504.           also available on CSERV.  It details the procedure for finding
  505.           out the location of this byte, using DEBUG.
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.           Shared Files in Turbo Pascal 5.0                                8
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.